<?php
session_start();
require_once '../models/Product.php';
require_once '../models/Customer.php';
require_once '../models/_check_admin.php';
$ordersID=$_REQUEST['ordersID'];
//lấy thông tin customer
$row=Product::getInfoOrders($ordersID);
$get=Product::getInfoProduct($row['productID']);

$_msg="";
$active="";
$revoke="";
if($row['or_status']=="active"){
	$revoke="<td><input type='submit' name='btn_revoke' value='Revoke' class='btn_submit'></td>";}
else if($row['or_status']=="revoke"){
	$active="<td><input type='submit' name='btn_active' value='Active' class='btn_submit'></td>";}
else if($row['or_status']=="pending"){
$active="<td><input type='submit' name='btn_active' value='Active' class='btn_submit'></td>";
$revoke="<td><input type='submit' name='btn_revoke' value='Revoke' class='btn_submit'></td>";
}

if(isset($_POST['btn_active'])){
$status="active";
$quantity=$get['quantity']-$row['or_quantity'];
$re=Product::updateStatusOrders($ordersID,$status,$quantity);
if($re){
	echo "<script>alert('Cập nhật thành công!');window.location='../admin/orders.php'</script>";}
else{
echo "<script>alert('Cập nhật thất bại!');window.location='../admin/orders.php'</script>";}
}
if(isset($_POST['btn_revoke'])){
$money=$money=$row['money']+$row['totalPrice'];
$re=Customer::payment($row['customerID'],$money);
$status="revoke";
$quantity=$row['or_quantity']+$get['quantity'];
$re=Product::updateStatusOrders($ordersID,$status,$quantity);

if($re){
	echo "<script>alert('Đơn đặt hàng đã được hủy bỏ!');window.location='../admin/orders.php'</script>";}
else{
echo "<script>alert('Cập nhật thất bại!');window.location='../admin/orders.php'</script>";}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Order</title>
<?php include '../admin/_meta.php';?>
</head>
<body>
<form method="post" action="">
	<div id="firstLine">
		<div id="top-bar">
		<?php include '_top_bar_admin.php';?>
		</div>
	</div>
	<div id="container">
		<div id="header">
			<?php include '_menu_admin.php';?>
		</div>
		<div id="center">
			<div id="menu-left">
				<?php //include '_menu_left_admin.php';?>
				
			</div>
			<div id="table-user">
				<table id="table-admin">
					<tr>
						<td>Name Customer</td>
						<td><label><?php echo $row['f_name'].' '.$row['l_name'];?></label></td>
					</tr>
					<tr>
					</tr>
					<tr>
						<td>Phone</td>
						<td><label><?php echo $row['phone'];?></label></td>
					</tr>
					<tr>
						<td>Email</td>
						<td><label><?php echo $row['email'];?></label></td>
					</tr>
					<tr>
						<td>Address</td>
						<td><label><?php echo $row['address'];?></label></td>
					</tr>
					<tr>
						<td>City</td>
						<td><label><?php echo $row['city'];?></label></td>
					</tr>
					<tr>
						<td>Date</td>
						<td><label><?php echo gmdate("d/m/Y H:i A",$row['or_dateAdd']+7*3600);?></label></td>
					</tr>
					<tr>
						<td>Total price</td>
						<td><label><?php echo number_format($row['totalPrice'],3).' VND';?></label></td>
					</tr>
					<tr>
						<td>Status</td>
						<td><label><?php echo $row['or_status'];?></label></td>
					</tr>
					<tr>
						<td colspan='2'><a href="../admin/orders_detail.php?ordersID=<?php echo $row['ordersID']?>">Click xem chi tiết</a></td>
					</tr>
					<tr>
						
						<?php echo $active;
							echo $revoke;?>
						<td><a href='orders.php'><input type='button' value='Cancel' class='btn_submit' /></a></td>
					</tr>
					<tr>
						<td colspan="2"><?php if(isset($_POST['btn_submit'])) echo $_msg; ?></td>
					</tr>
					</table>
			</div>
	</div>
	<div>
		<?php include '_footer_admin.php';?>
	</div>
</form>
</body>
</html>